package com.jboost.hyk.common.constant;

import com.baomidou.mybatisplus.annotation.EnumValue;
import com.fasterxml.jackson.annotation.JsonValue;
import com.jboost.common.constant.CommonConstant;
import com.jboost.common.constant.EnumInterface;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public enum BusinessStatsTypeEnum implements EnumInterface {
    // 机构统计维度 ：周-week；月-month；季-quarter；年-year；
    WEEK("week", "周"),
    MONTH("month", "月"),
    QUARTER("quarter", "季"),
    YEAR("year", "年");

    BusinessStatsTypeEnum(String code, String desc) {
        this.code = code;
        this.value = desc;
        this.desc = desc;
        this.start = null;
        this.end = null;
    }

    @EnumValue
    @JsonValue    //标记响应json值
    private final String code;
    private final String value;
    private final String desc;
    private Date start;
    private Date end;

    public Date getStart() {
        return start;
    }

    public Date getEnd() {
        return end;
    }

    public String getCode() {
        return code;
    }

    public String getValue() {
        return value;
    }

    public String getDesc() {
        return desc;
    }
    public static BusinessStatsTypeEnum getByCode(String code) {
        BusinessStatsTypeEnum[] enums = BusinessStatsTypeEnum.values();
        for (int i = 0; i < enums.length; i++) {
            if(enums[i].code.equals(code)){
                return enums[i];
            }
        }
        return null;
    }
    public static String getValueByCode(String code) {
        BusinessStatsTypeEnum status = BusinessStatsTypeEnum.getByCode(code);
        if(null == status){
            return CommonConstant.UNDIFIND_DEFALUT;
        }
        return status.getValue();
    }
    public static String getDescByCode(String code) {
        BusinessStatsTypeEnum status = BusinessStatsTypeEnum.getByCode(code);
        if(null == status){
            return CommonConstant.UNDIFIND_DEFALUT;
        }
        return status.getDesc();
    }

    @Override
    public String desc() {
        return this.desc;
    }

    @Override
    public Integer value() {
        return null;
    }

    @Override
    public String code() {
        return this.code;
    }


    public void setDate() {
        switch (this){
            case WEEK:
                //自然周-获取上一周的结束、开始时间
                Calendar calendarWeek = new GregorianCalendar();
                calendarWeek.setFirstDayOfWeek(Calendar.MONDAY);		//这里设置一周开始时间是星期一
                calendarWeek.setTime(new Date());//当天时间
                calendarWeek.set(Calendar.DAY_OF_WEEK, calendarWeek.getFirstDayOfWeek()); // 本周一时间

                calendarWeek.add(Calendar.DAY_OF_MONTH,-7);    //上周一时间
                calendarWeek.set(Calendar.HOUR_OF_DAY, 0);
                calendarWeek.set(Calendar.MINUTE, 0);
                calendarWeek.set(Calendar.SECOND, 0);
                this.start = calendarWeek.getTime();

                calendarWeek.set(Calendar.DAY_OF_WEEK, calendarWeek.getFirstDayOfWeek() + 6); // 上周日时间
                calendarWeek.set(Calendar.HOUR_OF_DAY, 23);
                calendarWeek.set(Calendar.MINUTE, 59);
                calendarWeek.set(Calendar.SECOND, 59);
                this.end = calendarWeek.getTime();
                break;
            case MONTH:
                //自然月-获取上一月的结束、开始时间
                Calendar calendarMonth = new GregorianCalendar();
                calendarMonth.add(Calendar.MONTH, -1);//上个月时间：当前时间减去一个月

                calendarMonth.set(Calendar.DAY_OF_MONTH,1);//上个月开始时间：设置为1号,当前日期既为月第一天
                calendarMonth.set(Calendar.HOUR_OF_DAY, 0);
                calendarMonth.set(Calendar.MINUTE, 0);
                calendarMonth.set(Calendar.SECOND, 0);
                this.start = calendarMonth.getTime();

                calendarMonth.set(Calendar.DAY_OF_MONTH, calendarMonth.getActualMaximum(Calendar.DAY_OF_MONTH));
                calendarMonth.set(Calendar.HOUR_OF_DAY, 23);
                calendarMonth.set(Calendar.MINUTE, 59);
                calendarMonth.set(Calendar.SECOND, 59);
                this.end = calendarMonth.getTime();
                break;
            case QUARTER:
                //自然季度-获取上一季的结束、开始时间
                Calendar calendarQuarter = Calendar.getInstance();
                calendarQuarter.add(Calendar.MONTH, -3);//上季时间
                int remainder = calendarQuarter.get(Calendar.MONTH)  % 3;

                System.out.println(calendarQuarter.get(Calendar.MONTH));
                int month = remainder != 0 ? calendarQuarter.get(Calendar.MONTH) - remainder: calendarQuarter.get(Calendar.MONTH);

                calendarQuarter.set(Calendar.MONTH, month);
                calendarQuarter.set(Calendar.DAY_OF_MONTH, calendarQuarter.getActualMinimum(Calendar.DAY_OF_MONTH));//月的第一天
                calendarQuarter.set(Calendar.HOUR_OF_DAY, 0);
                calendarQuarter.set(Calendar.MINUTE, 0);
                calendarQuarter.set(Calendar.SECOND, 0);
                this.start = calendarQuarter.getTime();

                calendarQuarter.add(Calendar.MONTH, 2);//加两个月就是季度的第3个月
                calendarQuarter.set(Calendar.DAY_OF_MONTH, calendarQuarter.getActualMaximum(Calendar.DAY_OF_MONTH));//月的最后一天就是季度最后一天
                calendarQuarter.set(Calendar.HOUR_OF_DAY, 23);
                calendarQuarter.set(Calendar.MINUTE, 59);
                calendarQuarter.set(Calendar.SECOND, 59);
                this.end = calendarQuarter.getTime();
                break;
            case YEAR:
                //自然年-获取上一年的结束、开始时间
                Calendar calendarYear =  Calendar.getInstance();
                calendarYear.add(Calendar.YEAR,-1);    //上年
                calendarYear.set(Calendar.DAY_OF_YEAR, calendarYear.getActualMinimum(Calendar.DAY_OF_YEAR));//年的第一天
                calendarYear.set(Calendar.HOUR_OF_DAY, 0);
                calendarYear.set(Calendar.MINUTE, 0);
                calendarYear.set(Calendar.SECOND, 0);
                this.start = calendarYear.getTime();

                calendarYear.set(Calendar.DAY_OF_YEAR, calendarYear.getActualMaximum(Calendar.DAY_OF_YEAR));//年的最后一天
                calendarYear.set(Calendar.HOUR_OF_DAY, 23);
                calendarYear.set(Calendar.MINUTE, 59);
                calendarYear.set(Calendar.SECOND, 59);
                this.end = calendarYear.getTime();
                break;
            default:

                break;
        }
        return;
    }
}
